package com.xd.hbsys.aqi;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Service
public class AQIService {

    public static String RETURN_NULL = "[]";

    @Resource(name = "hbJdbcTemplate")
    private JdbcTemplate hbJdbcTemplate;

    @Resource
    private AQIDao aqiDao;

    public Date getDateHour(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return format.parse(str);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    //返回某小时所有市的空气质量
    public String queryCityHour(String date, String city) {
        Date dt = getDateHour(date);
        if (dt == null) return RETURN_NULL;

        String sql = "select * from city_aqi_hour where time_code ='" + date + "'";
        if (city != null && city.length() > 0) {
            sql += " and city_name = '" + city + "'";
        }
        sql += ";";
        return _executeSQLHour(sql);

    }

    public String queryCityHourRange(String date0, String date1, String city) {
        Date dt = getDateHour(date0);
        if (dt == null) return RETURN_NULL;
        dt = getDateHour(date1);
        if (dt == null) return RETURN_NULL;

        String sql = "select * from city_aqi_hour where time_code >='" + date0 + "' and time_code <= '" + date1 + "'";
        if (city != null && city.length() > 0) {
            sql += " and city_name = '" + city + "'";
        }
        sql += ";";
        return _executeSQLHour(sql);
    }

    public String _executeSQLHour(String sql) {
        List<Map<String, Object>> rows = hbJdbcTemplate.queryForList(sql);
        Iterator it = rows.iterator();
        JSONArray arr = new JSONArray();
        while (it.hasNext()) {
            Map<String, Object> r = (Map<String, Object>) it.next();
            String name = (String) r.get("city_name");
            String citycode = (String) r.get("city_code");
            Timestamp ts = (Timestamp) r.get("time_code");
            String timecode = ts.toString();
            float aqi = (float) r.get("city_iaqi");
            float pm10 = (float) r.get("pm10");
            float pm25 = (float) r.get("pm25");
            float so2 = (float) r.get("so2");
            float no2 = (float) r.get("no2");
            float o3 = (float) r.get("o3");
            float co = (float) r.get("co");

            String air_status = (String) r.get("air_status");
            String primary_pollutant = (String) r.get("primary_pollutant");

            JSONObject obj = new JSONObject();
            obj.put("name", name);
            obj.put("code", citycode);
            obj.put("date", timecode);
            obj.put("aqi", aqi);
            obj.put("pm10", pm10);
            obj.put("pm25", pm25);
            obj.put("so2", so2);
            obj.put("no2", no2);
            obj.put("o3", o3);
            obj.put("co", co);
            obj.put("air_status", air_status);
            obj.put("primary_pollutant", primary_pollutant);
            arr.add(obj);
        }
        return arr.toJSONString();
    }

    public Date getDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return format.parse(str);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    //返回某天所有市的空气质量
    public String queryCityDay(String day, String city) {
        Date dt = getDate(day);
        if (dt == null) return RETURN_NULL;


        String sql = "select * from city_aqi_day where time_code ='" + day + "'";
        if (city != null && city.length() > 0) {
            sql += " and name = '" + city + "'";
        }
        sql += ";";
        return _executeSQL(sql);
    }

    public String queryCityDayRange(String day0, String day1, String city) {

        Date dt = getDate(day0);
        if (dt == null) return RETURN_NULL;
        dt = getDate(day1);
        if (dt == null) return RETURN_NULL;

        String sql = "select * from city_aqi_day where time_code >='" + day0 + "' and time_code <= '" + day1 + "'";
        if (city != null && city.length() > 0) {
            sql += " and name = '" + city + "'";
        }
        sql += ";";
        return _executeSQL(sql);
    }

    public String _executeSQL(String sql) {
        List<Map<String, Object>> rows = hbJdbcTemplate.queryForList(sql);
        Iterator it = rows.iterator();
        JSONArray arr = new JSONArray();
        while (it.hasNext()) {
            Map<String, Object> r = (Map<String, Object>) it.next();
            String name = (String) r.get("name");
            //String citycode = (String)r.get("city_code");
            Timestamp ts = (Timestamp) r.get("time_code");
            String timecode = ts.toString();
            float aqi = (float) r.get("aqi");
            float pm10 = (float) r.get("pm10");
            float pm25 = (float) r.get("pm25");
            float so2 = (float) r.get("so2");
            float no2 = (float) r.get("no2");
            float o3 = (float) r.get("o3");
            float co = (float) r.get("co");

            String air_level = (String) r.get("air_level");
            String air_status = (String) r.get("air_status");
            String primary_pollutant = (String) r.get("primary_pollutant");

            JSONObject obj = new JSONObject();
            obj.put("name", name);
            obj.put("date", timecode);
            obj.put("aqi", aqi);
            obj.put("pm10", pm10);
            obj.put("pm25", pm25);
            obj.put("so2", so2);
            obj.put("no2", no2);
            obj.put("o3", o3);
            obj.put("co", co);
            obj.put("air_level", air_level);
            obj.put("air_status", air_status);
            obj.put("primary_pollutant", primary_pollutant);
            arr.add(obj);
        }
        return arr.toJSONString();
    }

    public List<Map<String, Object>> findCityAqiByDay(Map<String, Object> map) {


        String t1 = map.get("t1").toString() + " 00:00:00";
        String t2 = map.get("t2").toString() + " 00:00:00";
        map.put("t1", t1);
        map.put("t2", t2);
        List<Map<String, Object>> list = new ArrayList<>();
        list = aqiDao.findCityAqiByDay(map);
        for (Map<String, Object> map1 : list) {
            map1.put("date", map1.get("date").toString().substring(0, 19));
        }


        return list;


    }

    public Map<String, Object> getMaxtimeAqiDay() {

        Map<String, Object> map = aqiDao.getMaxtimeAqiDay();

            map.put("time_code",map.get("time_code").toString().substring(0,10));

       return map;
    }
}
